package com.study.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.study.pojo.User;
import com.study.util.JsonUtils;
import lombok.ToString;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * UserController class
 *
 * @author chenyeqing
 * @date 2021/3/23
 */
@RestController
public class UserController {


    @GetMapping(value = "/j1", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String test01() throws JsonProcessingException {

        User user1 = new User("张一", 18, "男");

        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(user1);
        return s;
    }

    @GetMapping(value = "/j2", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String test02() throws JsonProcessingException {

        User user1 = new User("张一", 18, "男");
        User user2 = new User("张二", 18, "男");
        User user3 = new User("张三", 18, "男");
        User user4 = new User("张四", 18, "男");

        List<User> users = new ArrayList<User>();
        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(users);
        return s;
    }

    @GetMapping(value = "/j3", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String test03() {
        Date date = new Date();
        return JsonUtils.getJson(date, "yyyy-MM-dd hh:mm:ss");
    }

    @GetMapping(value = "/j4", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String test04() {

        User user1 = new User("张一", 18, "男");
        User user2 = new User("张二", 18, "男");
        User user3 = new User("张三", 18, "男");
        User user4 = new User("张四", 18, "男");

        List<User> users = new ArrayList<User>();
        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        return JsonUtils.getJson(users);
    }

    @PostMapping(value = "/j5")
    public String test05() {

        User user1 = new User("张一", 18, "男");
        User user2 = new User("张二", 18, "男");
        User user3 = new User("张三", 18, "男");
        User user4 = new User("张四", 18, "男");

        List<User> users = null;
        //users.add(user1);
        //users.add(user2);
        //users.add(user3);
        //users.add(user4);

        if (users.contains(user1)) {
            System.out.println(1);
        }


        return JSON.toJSONString(users);
    }


}
